package elearning.record.dao;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import elearning.course.entity.Course;
import elearning.record.entity.Record;
import elearning.user.entity.User;


public class RecordDao extends HibernateDaoSupport{

	@SuppressWarnings("unchecked")
	public Record findByUserIdAndCourseId(Integer userId, Integer courseId) {
		String hql = "from Record where userId = ? and courseId = ?";
		List<Record> rlist = this.getHibernateTemplate().find(hql,userId,courseId);
		if(rlist.size()>0&&rlist!=null){
			return rlist.get(0);
		}
		return null;
	}

	public void update(Record record) {
		this.getHibernateTemplate().update(record);
	}

	public void save(Record record) {
		
		this.getHibernateTemplate().save(record);
	}

	public Course findCourseByCourseId(Integer courseId) {
		
		return this.getHibernateTemplate().get(Course.class,courseId);
	}

	public void update(Course courseCollect) {
		this.getHibernateTemplate().update(courseCollect);
	}

	public List<Record> findRecordByUid(Integer uid) {
		String hql="from Record where userId = ?";
		@SuppressWarnings("unchecked")
		List<Record> list = this.getHibernateTemplate().find(hql,uid);
		if(list!=null&&list.size()>0){
			return list;
		}
		return null;
	}

	public int findnoteCountByCourseId(Integer courseId) {
		String hql ="select count(*) from Note where videoId in (select videoId from Video where courseId = ?)";
		List<Long> list = this.getHibernateTemplate().find(hql,courseId);
		if(list.size()>0&&list!=null)
			return list.get(0).intValue();
		return 0;
	}

	public int findquestionCountByCourseId(Integer courseId) {
		String hql ="select count(*) from Question where videoId in (select videoId from Video where courseId = ?)";
		List<Long> list = this.getHibernateTemplate().find(hql,courseId);
		if(list.size()>0&&list!=null)
			return list.get(0).intValue();
		return 0;
	}

	public List<Record> findAll() {
		String hql = "from Record";
		List<Record> list = this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null)
			return list;	
		return null;
	}

	public Integer findCourseCount() {
		String hql = "select count(*) from Course";
		List<Long> list =this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null)
			return list.get(0).intValue();
		return 0;
	}

	public Integer findUserCount() {
		String hql = "select count(*) from User";
		List<Long> list =this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null)
			return list.get(0).intValue();
		return 0;
	}

	public List<Course> findCourse() {
		String hql="from Course order by courseId";
		List<Course> list = this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null){
			return list;
		}
		return null;
	}

	public List<User> findUser() {
		String hql="from User order by userId";
		List<User> list = this.getHibernateTemplate().find(hql);
		if(list.size()>0&&list!=null){
			return list;
		}
		return null;
	}

	

	
}
